package com.ukao.cashregister.pesenter;

import android.content.Context;

import com.ukao.cashregister.base.BasePresenter;
import com.ukao.cashregister.bean.BaseBean;
import com.ukao.cashregister.bean.BatchlistBean;
import com.ukao.cashregister.bean.FactoryBatchBean;
import com.ukao.cashregister.bean.StringBean;
import com.ukao.cashregister.consts.Constant;
import com.ukao.cashregister.consts.SaveParamets;
import com.ukao.cashregister.retrofit.ApiCallback;
import com.ukao.cashregister.utils.CheckUtils;
import com.ukao.cashregister.view.BatchDetailsView;

import java.util.HashMap;

import rx.Observable;

/**
 * Created by ukaodev on 2017/6/26.
 */

public class BatchDetailsPesenter extends BasePresenter<BatchDetailsView> {

    public BatchDetailsPesenter(BatchDetailsView view) {
        attachView(view);
    }





    /**
     送厂批次详情
     */
    public void orderProductDetail(Context  mContext,String  id) {

        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("id",id);
        mHashMap.put("accessToken", SaveParamets.getToken(mContext));
        addSubscription(apiStores.orderProductDetail(Constant.createParameter(mHashMap)),
                new ApiCallback<FactoryBatchBean>() {
                    @Override
                    public void onSuccess(FactoryBatchBean model) {

                        if (model.getHttpCode() == 200) {

                               mvpView.loadTopSuccess(model.getData());

                        }else{
                            mvpView.loadFail(model.getMsg());

                        }
                    }




                    @Override
                    public void onFinish() {
                            mvpView.hideLoading();
                    }
                });
    }


/*送厂衣物列表*/
    public void atchInRellist( Context mContext,String id,int pageNum,int pageSize ) {

        HashMap mHashMap=new HashMap<String,String>();
      //  mHashMap.put("scanCode",scanCode);
        mHashMap.put("inId",id);
        mHashMap.put("pageNum",pageNum+"");
        mHashMap.put("pageSize",pageSize+"");
        mHashMap.put("accessToken", SaveParamets.getToken(mContext));
        addSubscription(apiStores.atchInRellist(Constant.createParameter(mHashMap)),
                new ApiCallback<BatchlistBean>() {
                    @Override
                    public void onSuccess(BatchlistBean model) {
                        if (model.getHttpCode() == 200) {

                            mvpView.loadlListSuccess(model.getData());

                        }else{
                            mvpView.loadFail(model.getMsg());

                        }


                    }


                    @Override
                    public void onFinish() {
                        mvpView.refreshFinish();
                    }

                });
    }

    /**
     衣物 添加
     */
    public void BatchInReladd( Context mContext, String id ,final String addid,String scanCode) {
          mvpView.showLoading();
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("inId",id);
        if(!CheckUtils.isEmpty(addid)) mHashMap.put("orderProIds", addid);
        if(!CheckUtils.isEmpty(scanCode))   mHashMap.put("scanCode",scanCode);
        mHashMap.put("accessToken", SaveParamets.getToken(mContext));
        Observable Observable;
        Observable = !CheckUtils.isEmpty(addid) ? apiStores.BatchInReladd(Constant.createParameter(mHashMap)) : apiStores.searchBatchInReladd(Constant.createParameter(mHashMap));
        addSubscription(Observable,
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {
                        if (model.getHttpCode() == 200) {
                            mvpView.reladdSuccess(!CheckUtils.isEmpty(addid) ? false : true);


                        }else{
                           mvpView.loadFail(model.getMsg());

                        }


                    }


                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }
    /**
     衣物 删除
     */
    public void BatchInReldelete( Context mContext,String id ) {
           mvpView.showLoading();
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("ids",id);
        mHashMap.put("accessToken", SaveParamets.getToken(mContext));
        addSubscription(apiStores.BatchInReldelete(Constant.createParameter(mHashMap)),
                new ApiCallback<BaseBean>() {
                    @Override
                    public void onSuccess(BaseBean model) {
                        if (model.getHttpCode() == 200) {

                              mvpView.deleteSuccess("删除成功");

                        }else{
                            mvpView.loadFail(model.getMsg());

                        }


                    }


                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }



    /**
     衣物 移入批次
     */
    public void batchshift( Context mContext,String  id,String productIds ) {
        mvpView.showLoading();
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("inId",id);
        mHashMap.put("orderProIds",productIds);
        mHashMap.put("accessToken", SaveParamets.getToken(mContext));
        addSubscription(apiStores.batchshift(Constant.createParameter(mHashMap)),
                new ApiCallback<BaseBean>() {
                    @Override
                    public void onSuccess(BaseBean model) {
                        if (model.getHttpCode() == 200) {

                            mvpView.moveSuccess();

                        }else{
                            mvpView.loadFail(model.getMsg());

                        }


                    }


                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }




    /**
     进厂批次完成
     */
    public void atchIncomplete(final Context mContext,final  String  id ) {
        mvpView.showLoading();
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("id",id);
        mHashMap.put("accessToken", SaveParamets.getToken(mContext));
        addSubscription(apiStores.atchIncomplete(Constant.createParameter(mHashMap)),
                new ApiCallback<BaseBean>() {
                    @Override
                    public void onSuccess(BaseBean model) {
                        if (model.getHttpCode() == 200) {

                            orderProductDetail(mContext,id);
                           mvpView.batchFinish("批次完成");

                        }else{
                            mvpView.loadFail(model.getMsg());

                        }


                    }


                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }


    /**
     * 批次 删除

     */
    public void batchIndelete( Context mContext,String id) {
        mvpView.showLoading();
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("id",id);
        mHashMap.put("accessToken", SaveParamets.getToken(mContext));
        addSubscription(apiStores.batchIndelete(Constant.createParameter(mHashMap)),
                new ApiCallback<BaseBean>() {
                    @Override
                    public void onSuccess(BaseBean model) {
                        if (model.getHttpCode() == 200) {

                            mvpView.deleteBatchSuccess("删除成功");

                        }else{
                            mvpView.loadFail(model.getMsg());

                        }


                    }


                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }
}
